﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
namespace AsteroidWar
{
    class ShotManager
    {
        public List<Sprite> Shots = new List<Sprite>();
        private Rectangle screenBounds;

        private static Texture2D Texture;
        private static Rectangle InitialFrame;
        private static int FrameCount;
        private static int CollisionRadius;
        private float shotSpeed;

        public ShotManager
            (Texture2D texture, 
            Rectangle initialFrame, 
            int frameCount, 
            int collisionRadius, 
            float shotSpeed, 
            Rectangle screenBounds
           )
        {
            Texture = texture;
            InitialFrame = initialFrame;
            FrameCount = frameCount;
            CollisionRadius = collisionRadius;
            this.shotSpeed = shotSpeed;
            this.screenBounds = screenBounds;      
        }

        public void FireShot(
            Vector2 location,
            Vector2 velocity,
            bool PlayerFired)
        {
            Sprite shot = new Sprite(location, Texture, InitialFrame, velocity);

            for (int x = 1; x < FrameCount; x++)
            {
                shot.AddFrame(new Rectangle(
                    InitialFrame.X + (InitialFrame.Width * x),
                    InitialFrame.Y,
                    InitialFrame.Width,
                    InitialFrame.Height));
            }

            shot.CollisionRadius = CollisionRadius;
            shot.Velocity *= shotSpeed;

            Shots.Add(shot);
            
            if (PlayerFired)
            {
                Sound.PlayPlayerShot();
            }
            else
            {
                Sound.PlayEnemyShot();
            }
        }

        public void Update(GameTime gametime)
        {
            for (int x = Shots.Count - 1; x >= 0; x--)
            {
                Shots[x].Update(gametime);
                //Remove shot that move off the screen
                if (!screenBounds.Intersects(Shots[x].Destination))
                    Shots.RemoveAt(x);
            }
        }

       

        public void Draw(SpriteBatch spriteBatch)
        {
            foreach (Sprite fireShoot in Shots)
            {
                fireShoot.Draw(spriteBatch,0.5f);
            }
        }
    }
}
